import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * This class makes reference to the hero of the game, who the user will use to play.
 * 
 * @author (Arturo Aaron Rios Obregon) 
 * @version (25 june 2013)
 */
public class Hero extends Actor
{
    private Bala arma;
    private SimpleTimer timer;
    private int vida;
    private int munition;
    private int bombas;
    private GreenfootSound s;
    private GreenfootImage [] images;
    private int numImage;
    
    /**
     * Constructor
     */
    public Hero()
    {
        arma = new SimpleBullet();
        timer = new SimpleTimer();
        timer.mark();
        munition=0;
        bombas = 10;
        vida = 200;
        s = new GreenfootSound("metal_slug_heavymachinegun.mp3");
        images = new GreenfootImage[7];
        images[0] = new GreenfootImage("Frame8.png");
        images[1] = new GreenfootImage("Frame9.png");
        images[2] = new GreenfootImage("Frame10.png");
        images[3] = new GreenfootImage("Frame11.png");
        images[4] = new GreenfootImage("Frame12.png");
        images[5] = new GreenfootImage("Frame13.png");
        images[6] = new GreenfootImage("Frame14.png");
        numImage=0;
    }
    
    /**
     * Its the act method for the hero.
     */
    public void act() 
    {
        scroll();        
        tocaItem();
        tocaBalaEnemigo();
        revisa();
        
    }   
    
    /**
     * create a explosion effect.
     */
    public void explota()
    {
        if(numImage!=6)
            imagenes();
    }
    
    /**
     * change the images for the explosion effect.
     */
    public void imagenes()
    {                
        numImage+=1;
        setImage(images[numImage]);
    }
    
    /**
     * check the life and munition of the player.
     */
    public void revisa()
    {
        if(vida<0)
            vida=0;
        if(munition<0)
            munition=0;
    }
    
    
    /**
     * return the number of bombs that the player have at this moment.
     */
    public int getBomb()
    {
        return bombas;
    }
    
    /**
     * return the number of munition that the player have at this moment.
     */
    public int getAmmo()
    {
        return munition;
    }    
    
    /**
     * return the number of life points that the player have at this moment.
     */
    public int getVida()
    {
        return vida;
    }
    
    /**
     * methos which check if the hero touch a enemy bullet.
     */
    public void tocaBalaEnemigo()
    {
        if(getOneIntersectingObject(BulletEnemy.class)!=null)
        {
            vida-=10;
            getWorld().removeObject(getOneIntersectingObject(BulletEnemy.class));
        }
        if(getOneIntersectingObject(Meteor.class)!=null)
        {
            vida-=10;
            getWorld().removeObject(getOneIntersectingObject(Meteor.class));
        }
        if(getOneIntersectingObject(LaserMadre.class)!=null)
        {
            vida-=20;
        }
    }

    
    /**
     * This method check if the player touch an item and depending of the item 
     * kind will change the weapon or the munition of the player.
     */
    public void tocaItem()
    {
        if(getOneIntersectingObject(R.class)!=null)
        {
            if(!(arma instanceof Rocket))
            {
                arma= new Rocket();
                munition=((Item)getOneIntersectingObject(R.class)).getCantidad();
            }
            else 
                munition+=20;
            s = new GreenfootSound("metal_slug_rocketlauncher.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(R.class));
        }
        if(getOneIntersectingObject(H.class)!=null)
        {
            if(!(arma instanceof Bullet))
            {
                arma= new Bullet();
                munition=((Item)getOneIntersectingObject(H.class)).getCantidad();
            }
            else 
                munition+=35;
            s = new GreenfootSound("metal_slug_heavymachinegun.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(H.class));
        }
        if(getOneIntersectingObject(L.class)!=null)
        {
            if(!(arma instanceof Laser))
            {
                arma= new Laser();
                munition=((Item)getOneIntersectingObject(L.class)).getCantidad();
            }
            else 
                munition+=5;
            s = new GreenfootSound("metal_slug_lasergun.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(L.class));
        }
        if(getOneIntersectingObject(Ammo.class)!=null)
        {
            if(munition!=0)
            {
                if(arma instanceof Rocket)
                    munition+=20;
                else if(arma instanceof Bullet)
                    munition+=35;
                else if(arma instanceof Laser)
                    munition+=5;    
            }
            s = new GreenfootSound("metal_slug_ok.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(Ammo.class));
        }
        if(getOneIntersectingObject(Bomb.class)!=null)
        {
            bombas+=((Item)getOneIntersectingObject(Bomb.class)).getCantidad();
            s = new GreenfootSound("metal_slug_ok.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(Bomb.class));
        }
        if(getOneIntersectingObject(Gas.class)!=null)
        {
            vida+=((Item)getOneIntersectingObject(Gas.class)).getCantidad();
            s = new GreenfootSound("metal_slug_ok.mp3");
            s.play();
            ((Juego)getWorld()).aumentaPuntos(50);
            getWorld().removeObject(getOneIntersectingObject(Gas.class));
        }
        if(getOneIntersectingObject(Shield.class)!=null)
        {
            getWorld().removeObject(getOneIntersectingObject(Shield.class));
            s = new GreenfootSound("metal_slug_ok.mp3");
            s.play();
            Escudo s = new Escudo();
            getWorld().addObject(s,getX(),getY());
            ((Juego)getWorld()).aumentaPuntos(50);
        }
    }
    
    /**
     * Method which check the keys touched by the player and emit an action depending of the key.
     */
    public void scroll()
    {
        int x = super.getX();
        int y = super.getY();
        if(Greenfoot.isKeyDown("up"))
        {
            if(y>0)
                setLocation(x,y-7);
        }
        if(Greenfoot.isKeyDown("down"))
        {
            if(y<getWorld().getHeight()-1)
            setLocation(x,y+7);
        }
        if(Greenfoot.isKeyDown("left"))
        {
            if(x>0)
            setLocation(x-7,y);
        }
        if(Greenfoot.isKeyDown("right"))
        {
            if(x<getWorld().getWidth())
            setLocation(x+7,y);
        }// Add your action code here.
        if(Greenfoot.isKeyDown("space"))
        {
            if((timer.millisElapsed()/150)>2)
            {
                dispara();
                timer.mark();
            }
        }
        if(Greenfoot.isKeyDown("z"))
        {
             if((timer.millisElapsed()/150)>2)
             {
                 disparaMisil();
                 timer.mark();
             }
        }
    }
    
    /**
     * Method that will paint a missile on the position of the ship.
     */
    public void disparaMisil()
    {
        World mundo = getWorld();
        
        int x=getX(), y= getY();
        Misil ms = new Misil();
        if(bombas>0)
        {
            mundo.addObject(ms,x,y-54);
            bombas=bombas-1;
        }
    }
    
    /**
     *Method which will create the bullets at the moment when the keys was touched.
     */
    public void dispara()
    {
        World mundo = getWorld();
        
        int x=getX(), y= getY();
        if(munition==0)
        {            
            arma = new SimpleBullet();
            mundo.addObject(arma,x,y-54);
        }
        else if(arma instanceof Rocket)
        {
                arma = new Rocket();
                mundo.addObject(arma,x-25,y-28);
                arma = new Rocket();
                mundo.addObject(arma,x+22,y-28);
                munition=munition-2;
        }
        else if(arma instanceof Bullet)
        {
                arma = new Bullet();
                mundo.addObject(arma,x-30,y-32);                             
                arma = new Bullet();
                mundo.addObject(arma,x-24,y-12);
                arma = new Bullet();
                mundo.addObject(arma,x-18,y-22); 
                arma = new Bullet();
                mundo.addObject(arma,x+29,y-32);                             
                arma = new Bullet();
                mundo.addObject(arma,x+23,y-12);
                arma = new Bullet();
                mundo.addObject(arma,x+17,y-22); 
                munition=munition-2;
        }
        else if(arma instanceof Laser)
        {
                arma = new Laser();
                mundo.addObject(arma,x-25,y-251);
                arma = new Laser();
                mundo.addObject(arma,x+23,y-251);
                munition=munition-1;
        }
    }
}
